//
//  BusinessPlatformVC.m
//  reSearchDemo
//
//  Created by 云库 on 15/9/26.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

//商家平台
/**
 * 16. 商家商品列表：
 http://mall.yunku.mobi:8080/mall/html/ashop/nrjgoods.do?id=56&pageno=1     ===ok===
 get
 id 商家id
 */



#import "BusinessPlatformVC.h"
#import "AFNetworking.h"
#import "MJExtension.h"
#import "UIView+Category.h"
#import "MJRefresh.h"
#import "BusinessGoodsList.h"
#import "MBProgressHUD.h"
#import "GoodsSortTableViewCell.h"
#import "GoodsSort.h"
#import "GoodsDetail.h"
#import "GoodsDetailVC.h"

@interface BusinessPlatformVC ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableView;

@property (nonatomic,assign) int pageno;
@property (strong, nonatomic) MBProgressHUD *hub;
/** 商品列表*/
@property (strong, nonatomic) NSMutableArray *goodsListArr;

@end

@implementation BusinessPlatformVC


- (NSMutableArray *)goodsListArr{
    if (!_goodsListArr) {
        _goodsListArr = [NSMutableArray array];
    }
    return _goodsListArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置标题
    self.title = self.shopName;
    
    [self setUpTableView];
    //刷新控件（创建tableView后才刷新）
    [self setupDownRefresh];
    [self setupUpRefresh];
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hub.labelText = @"正在拼命加载中....";
    self.hub.dimBackground = YES;
}

/**
 * 创建tableView
 */
- (void)setUpTableView{
    
    //2.创建table view
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
    
}


/** 下拉刷新*/
- (void)setupDownRefresh
{
    // 1.添加刷新控件
    [self.tableView addHeaderWithTarget:self action:@selector(loadNewDeals)];
    
    // 2.进入刷新状态
    [self.tableView headerBeginRefreshing];
    
}

/** 上拉刷新*/
- (void)setupUpRefresh
{
    [self.tableView addFooterWithTarget:self action:@selector(loadMoreDeals)];
}


//加载新的订单
- (void)loadNewDeals{
    self.pageno = 1;
    [self getDataFromServer];
}

- (void)loadMoreDeals{
    
    //设置其他参数
    self.pageno++;
    //重新发送请求
    [self getDataFromServer];
}




/** 获取数据*/
- (void)getDataFromServer{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"pageno"] = @(self.pageno);
    param[@"id"] = @(self.shop_id);
    NSString *path = [NSString stringWithFormat:@"%@%@",SHOPER,@"html/ashop/nrjgoods.do"];
    [manager GET:path parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self.hub hide:YES];
        
        if (self.pageno == 1) {
            //将之前旧的数据清空
            [self.goodsListArr removeAllObjects];
        }
        
        NSArray *array = [BusinessGoodsList objectArrayWithKeyValuesArray:responseObject[@"data"]];
    
        [self.goodsListArr addObjectsFromArray:array];
        [self.tableView reloadData];
        
        // 结束刷新刷新
        [self.tableView headerEndRefreshing];
        // 结束刷新刷新
        [self.tableView footerEndRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 结束刷新刷新
        [self.tableView headerEndRefreshing];
        [self.tableView footerEndRefreshing];
        DLog(@"请求网络失败%@",self.class);
    }];

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - **************** 代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.goodsListArr.count;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"GoodsSortTableViewCell";
    GoodsSortTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell = [[[NSBundle mainBundle]loadNibNamed:@"GoodsSortTableViewCell" owner:self options:nil]lastObject];
    }
    GoodsSort * goods = self.goodsListArr[indexPath.row];
#warning TODO
    cell.deals = goods;
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //弹出详细商品视图
    GoodsDetailVC *goodsDetailVC = [[GoodsDetailVC alloc] init];
    //取出对应的参数传递
    BusinessGoodsList * goods = self.goodsListArr[indexPath.row];
    goodsDetailVC.goodId = goods.goodId;//以此查看详情
    goodsDetailVC.shopName = self.shopName;//title
    goodsDetailVC.shop_id = self.shop_id;//以此进入商家平台
    
    [self pushViewController:goodsDetailVC];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}



@end
